package com.carkeeper.controller;

import com.carkeeper.dto.ComponentDTO;
import com.carkeeper.dto.UserDTO;
import com.carkeeper.pojo.ComponentPO;
import com.carkeeper.pojo.StoreUserPO;
import com.carkeeper.service.ComponentService;
import com.carkeeper.vo.ComponentVO;
import com.carkeeper.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author ：XXX
 * @date ：Created in 2020/6/3
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/component")
public class ComponentController {
    @Autowired
    private ComponentService componentService;

    /**
     * 根据ID查询配件
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ComponentVO queryComponentById(@PathVariable Long id) {
        return componentService.queryComponentById(id);

    }

    /**
     * 增加配件
     *
     * @param componentPO
     * @return
     */
    @PostMapping
    public Boolean save(@RequestBody ComponentPO componentPO) {
        componentService.add(componentPO);
        return true;
    }

    /**
     * 据ID删除配件
     *
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public Boolean deleteUser(@PathVariable("id") Long id) {
        componentService.delete(id);
        return true;
    }

    /**
     * 修改配件信息
     *
     * @param componentPO
     * @return
     */
    @PutMapping("/{id}")
    public Boolean updateComponent(@PathVariable(value = "id") Long id, @RequestBody ComponentPO componentPO) {
        componentService.update(componentPO);
        return true;
    }

    /**
     * 获取配件分页数据
     *
     * @param page              页码
     * @param pageSize          页尺寸
     * @param storeName         商家名称
     * @param code              配件编码
     * @param componentName     配件名称
     * @param specification     规格
     * @param componenBrandName 配件品牌名称
     * @param componentTypeName 配件分类名称
     * @return 配件分页数据
     */
    @GetMapping
    public Pager<StoreUserPO> queryPage(@RequestParam(name = "page", defaultValue = "1") Long page,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Long pageSize,
                                        @RequestParam(name = "storeName", required = false) String storeName,
                                        @RequestParam(name = "code", required = false) String code,
                                        @RequestParam(name = "componentName", required = false) String componentName,
                                        @RequestParam(name = "specification", required = false) String specification,
                                        @RequestParam(name = "componenBrandNamee", required = false) String componenBrandName,
                                        @RequestParam(name = "state", required = false) Integer state,
                                        @RequestParam(name = "componentTypeName", required = false) String componentTypeName) {
        return new Pager(componentService.query(page, pageSize, storeName, code, componentName, specification, componenBrandName, componentTypeName, state));
    }

    @PostMapping("/list")
    public List<ComponentDTO> getByIds(@RequestBody List<Long> ids) {
        return componentService.getByIds(ids);
    }
}
